<template>
	<view>
		<view class="login">
			<!-- f1:用户名输入框 -->
			<view class="input-group">
				<uni-easyinput type="text" v-model="uname" :inputBorder="false" placeholder="手机号/邮箱" />
			</view>
			<!-- f2:密码输入框 -->
			<view class="input-group">
				<uni-easyinput :inputBorder="false" type="password" v-model="upwd" placeholder="请输入密码" />
			</view>
			<!-- 登录按钮 -->
			<view @click="login" class="button">
				登录
			</view>
			<!-- 打开注册组件的文字 -->
			<text class="go" @click="goRegister">还没账号？现在去注册</text>
		</view>
	</view>
</template>

<script>
	// import md5 from "../../md5.js";
	export default {
		data() {
			return {
				uname: '',
				upwd: ''
			};
		},
		methods: {
			login() {
				console.log(this.uname,this.upwd);
				// let name=md5.hex_md5(this.uname)
				// let pwd= md5.hex_md5(this.upwd);
				let name=this.uname;
				let pwd = this.upwd;
				console.log(name,pwd);
				if (!this.uname) {
					uni.showToast({
						title: '用户名为空',
						icon: 'error',
						duration: 1000
					})
					return
				}
				if (!this.upwd) {
					uni.showToast({
						title: '密码为空',
						icon: 'error',
						duration: 1000
					})
					return
				}
				console.log(5);
				uni.request({
					url: this.apiServer + 'applogin',
					data: {
						name: name,
						pwd: pwd
					},
					sslVerify:false,
					header: {
						'content-type': "application/x-www-form-urlencoded"
					},
					method: 'POST',
					success: (res) => {
						console.log(2);
						console.log(res);
						if (res.data.code == 201) {
							uni.showToast({
								title: "用户名或密码错误！",
								icon:"error"
							})
							return
						}
						if (res.data.code == 200) {
							console.log(res.data);
							let data = res.data.result[0];
							uni.setStorageSync('SUID',data.u_id);
							uni.setStorageSync('SNAME',data.u_name);
							uni.setStorageSync('PHONE',data.u_phone);
							uni.setStorageSync('EMAIL',data.u_email);
							uni.setStorageSync('SFACE',data.u_face);
							uni.showToast({
								title:'登录成功！',
								icon:"success"
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1500)
						}
					},
					fail: (err) => {
						console.log(err);
					},
					complete: () => {
						console.log(6);
					}
				})
			},
			goRegister() {
				uni.redirectTo({
					url: "../register/register"
				})
			}
		},
		mounted() {
			uni.setNavigationBarTitle({
				title: '用户登录'
			})
		}
	}
</script>

<style lang="scss">
	.login {
		position: absolute;
		left: 0;
		top: 0;
		box-sizing: border-box;
		width: 750rpx;
		padding: 5px 15px;

		.input-group {
			border-bottom: 1px solid #ddd;
			padding: 3px;
			display: flex;
			align-items: center;
			color: #999;
			font-size: 12px;

			text {
				margin-right: 5px;
			}

			input {
				font-size: 12px;
			}
		}

		.button {
			background-color: #0AA1ED;
			color: white;
			text-align: center;
			border-radius: 6px;
			font-size: 16px;
			line-height: 35px;
			margin-top: 18px;
			margin-bottom: 15px;

			&:active {
				background-color: rgba(10, 161, 237, 0.8);
			}
		}
		uni-easyinput{
		    width: 730rpx;
		}

		.go {
			cursor: pointer;
			color: #999;

			&:active {
				color: #0aa1ed;
			}
		}
	}
</style>
